﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChessGameV2.AI;

namespace ChessGameV2
{
    public enum MoveType { Standard, CastleQueenSide, CastleKingSide, PawnPromotion, EnPassent, NullMove};
    
    public class Move
    {
        int start;
        int end;
        int player;
        int _valueEnd;
        private int score = 0;
        //private string history=null;
        private MoveType _moveType;

        public Move()
        {
            this.start = int.MinValue;
            this.end = int.MinValue;
            this._valueEnd = int.MinValue;
            this.player = 0;
            this._moveType = MoveType.NullMove;            
        }
        public Move(int _start, int _end, int _player, int[] board)
        {
            this.start = _start;
            this.end = _end;
            this.player = _player;
            this._valueEnd = int.MinValue;
            this._moveType = MoveType.Standard;
            if ((board[_start] % 10 == 1) && ((_end < 29) || (_end > 90)))
                this._moveType = MoveType.PawnPromotion;
        }
        public Move(int _start, int _end, int _player,int[] board, MoveType _pl)
        {
            this.start = _start;
            this.end = _end;
            this.player = _player;
            this._valueEnd = int.MinValue;
            this._moveType = _pl;
            if ((board[_start] % 10 == 1) && ((_end < 29) || (_end > 90)))
                this._moveType = MoveType.PawnPromotion;
        }

        #region properties
        /// <summary>
        /// Diem cua quan co
        /// </summary>
        public int Score
        {
            get
            {
                return this.score;
            }
            set
            {
                this.score = value;
            }
        }
        /// <summary>
        /// Loai di chuyen
        /// </summary>
        public MoveType moveType
        {
            get
            {
                return _moveType;
            }
            set
            {
                _moveType = value;
            }
        }   
        /// <summary>
        /// Vi tri bat dau di chuyen
        /// </summary>
        public int Start
        {
            get
            {
                return start;
            }
            set
            {
                this.start = value;
            }
        }
        /// <summary>
        /// Vi tri di chuyen toi
        /// </summary>
        public int End
        {
            get
            {
                return end;
            }
            set
            {
                this.end = value;
            }
        }
        /// <summary>
        /// Quan co sinh di chuyen
        /// </summary>
        public int Player
        {
            get
            {
                return player;
            }
            set
            {
                this.player = value;
            }
        }
        /// <summary>
        /// Quan co tai trang thai cuoi
        /// </summary>
        public int valueEnd
        {
            get
            {
                return _valueEnd;
            }
            set
            {
                this._valueEnd = value;
            }
        }
        #endregion

        private string getStringBoard(int[] board)
        {
            string _return = "";
            foreach (int i in board)
                _return += i.ToString();
            return _return;
        }
        /// <summary>
        /// Thuc hien viec di chuyen tren ban co
        /// </summary>
        /// <param name="board"></param>
        public void ExecuteMove(int[] board)
        {
            //this.history = getStringBoard(board);
            if (this._moveType == MoveType.Standard)
            {
                this.valueEnd = board[this.end];
                board[this.end] = board[this.start];
                board[this.start] = 0;
                return;
            }

            if (this._moveType == MoveType.CastleKingSide)
            {
                board[this.start + 1] = board[this.start + 3] % 100;
                board[this.start + 3] = 0;

                board[this.start + 2] = board[this.start] % 100;
                board[this.start] = 0;
                return;
            }

            if (this.moveType == MoveType.CastleQueenSide)
            {
                //move rook
                board[this.start - 1] = board[this.start - 4] % 100;
                board[this.start - 4] = 0;

                board[this.start - 2] = board[this.start] % 100;
                board[this.start] = 0;
                return;
            }
            if (this._moveType == MoveType.PawnPromotion)
            {
                this.valueEnd = board[this.end];
                board[this.end] = board[this.start];
                board[this.start] = 0;
                board[this.end] += 4;
            }
        }
        /// <summary>
        /// Di chuyen lai trang thai truoc do
        /// </summary>
        /// <param name="board"></param>
        public void undo(int[] board)
        {
            if (this.moveType == MoveType.Standard)
            {
                board[this.start] = board[this.end];
                board[this.end] = this._valueEnd;
                return;
            }
            if (this._moveType == MoveType.CastleKingSide)
            {
                board[this.start] = board[this.start + 2] + 100;
                board[this.start + 2] = 0;

                board[this.start + 3] = board[this.start + 1] + 100;
                board[this.start + 1] = 0;
                return;
            }

            if (this.moveType == MoveType.CastleQueenSide)
            {
                board[this.start] = board[this.start - 2] + 100;
                board[this.start - 2] = 0;
                //tackeback
                board[this.start - 4] = board[this.start - 1] + 100;
                board[this.start - 1] = 0;
                return;
            }

            if (this._moveType == MoveType.PawnPromotion)
            {
                board[this.end] -= 4;
                board[this.start] = board[this.end];
                board[this.end] = this._valueEnd;
                return;
            }
        }
    }
}
